from datetime import datetime

from wtforms import ValidationError

from common.json_util import to_dicts
from common.oss_upload import replace_file, get_file_url
from daos.image_library_dao import *
from models.biz_enum import SystemLogModule, ImageLibraryStatus
from rules import db_commit, PAGINATION_PAGE_SIZE
from rules.system_log_rule import create_system_log


def search_image_library(pk):
    result = query_image_library(pk)
    return result


def search_image_library_by_title(title):
    result = query_image_library_by_title(title)
    return result


def search_image_library_with_page(page_index, data_param):
    data_param['page'] = page_index
    data_param['page_size'] = PAGINATION_PAGE_SIZE
    result = query_image_library_with_page(data_param)
    item_dicts = to_dicts(result.items)
    # for item in item_dicts:
    #     item['file_url'] = get_file_url(item.get('file_name'))
    result.items = item_dicts
    return result


def search_image_library_with_list(data_param):
    if not data_param.get('status'):
        data_param['status'] = ImageLibraryStatus.NORMAL.value

    result = to_dicts(query_image_library_s(data_param))
    for item in result:
        item['file_url'] = get_file_url(item.get('file_name'), expires=600)
    return result


def create_image_library(form, current_user):
    is_commit = False

    image_library_orm = ImageLibrary()

    image_library_orm.title = form.title.data
    image_library_orm.file_name = form.file_name.data
    image_library_orm.dy_open_pic_id = ''
    image_library_orm.status = ImageLibraryStatus.NORMAL.value
    image_library_orm.remarks = form.remarks.data

    result = add_image_library(image_library_orm, is_commit)

    # 添加系统日志
    operation_detail_dict = dict(before_operation=None, after_operation=image_library_orm.to_dict())
    log_dict = dict(module=SystemLogModule.CREATE_IMAGE_LIBRARY.value, operation_detail=operation_detail_dict,
                    operator=current_user.id)
    create_system_log(log_dict, is_commit)

    if not is_commit:
        db_commit()

    return result.to_dict()


def describe_image_library(pk, form, current_user):
    is_commit = False
    now = datetime.now()
    image_library_orm = query_image_library(pk)
    if image_library_orm:
        before_operation = image_library_orm.to_dict()

        need_update_list = ["last_update_time"]
        if form.title.data != image_library_orm.title:
            c = query_image_library_count(dict(title=form.title.data))
            if c > 0:
                raise ValidationError('标题已存在')

            image_library_orm.title = form.title.data
            need_update_list.append("title")

        if form.file_name.data != image_library_orm.file_name:
            # 更新bucket文件
            replace_file(image_library_orm.file_name, form.file_name.data)

        if form.remarks.data != image_library_orm.remarks:
            image_library_orm.remarks = form.remarks.data
            need_update_list.append("remarks")

        image_library_orm.last_update_time = now

        update_image_library(image_library_orm, *need_update_list, is_commit=is_commit)

        # 添加系统日志
        operation_detail_dict = dict(before_operation=before_operation, after_operation=image_library_orm.to_dict())
        log_dict = dict(module=SystemLogModule.UPDATE_IMAGE_LIBRARY, operation_detail=operation_detail_dict,
                        operator=current_user.id)
        create_system_log(log_dict, is_commit)

        if not is_commit:
            db_commit()

    return
